package com.cfpamf.athean.rabbit.mq.plus.domain.builder;

import java.time.Duration;
import java.time.LocalDateTime;

import com.cfpamf.athean.rabbit.mq.plus.common.enums.ProcessStatusEnum;
import com.cfpamf.athean.rabbit.mq.plus.domain.dto.RabbitMqPlusConsumerDTO;
import com.cfpamf.athean.rabbit.mq.plus.domain.dto.RabbitMqPlusConsumerDetailDTO;
import com.cfpamf.athean.rabbit.mq.plus.domain.po.RabbitMqPlusConsumerTaskPO;
import com.cfpamf.athena.utils.DateUtils;
import com.cfpamf.athena.utils.JSONUtils;
import com.cfpamf.athena.utils.ObjectUtils;

/**
 * 类RabbitMqPlusConsumerTaskBuilder.java的实现描述：
 *
 * @author Andy（陈洪桥）2022年12月12 12:36
 */
public class RabbitMqPlusConsumerTaskBuilder {

    public static RabbitMqPlusConsumerTaskPO buildFirstConsumerTaskRecord(int retryCount,
        RabbitMqPlusConsumerDTO consumerDTO, LocalDateTime firstConsumerTime,String messageId, String data) {
        RabbitMqPlusConsumerTaskPO consumerTaskPO = new RabbitMqPlusConsumerTaskPO();
        ObjectUtils.copyPropertys(consumerDTO, consumerTaskPO);
        consumerTaskPO.setConsumerTime(firstConsumerTime);
        consumerTaskPO.setRetryCount(retryCount);
        consumerTaskPO.setFirstConsumerTime(firstConsumerTime);
        consumerTaskPO.setNextExecuteTime(
            DateUtils.addMinute(consumerTaskPO.getConsumerTime(), consumerDTO.getRetryDurationByMinute()));
        consumerTaskPO.setStatusCode(ProcessStatusEnum.TODO.getCode());
        consumerTaskPO.setStatusDesc(ProcessStatusEnum.TODO.getDesc());
        consumerTaskPO.setConsumerListenerImplClassName(  consumerDTO.getRabbitMqPlusConsumerListener().getClass().getSimpleName());
        RabbitMqPlusConsumerDetailDTO body= new RabbitMqPlusConsumerDetailDTO();
        ObjectUtils.copyPropertys(consumerDTO, body);
        body.setMessageId(messageId);
        body.setMessageBody(data);
        consumerTaskPO.setMessageBody(JSONUtils.beanToJson(body));
        return consumerTaskPO;
    }
    
    
    
    public static RabbitMqPlusConsumerTaskPO
        buildConsumerProcessorFailureTaskRecord(int retryCount,
        RabbitMqPlusConsumerDTO consumerDTO, LocalDateTime firstConsumerTime, String data) {
        // 业务处理失败
        LocalDateTime now = LocalDateTime.now();
        // 当前时间与首次消息发送的时间差
        long duration = Duration.between(firstConsumerTime, now).toMillis();
        // 消息补偿时间=当前时间+当前时间与首次消息发送的时间差
        LocalDateTime nextExecuteTime = DateUtils.addMillisecond(now, (int)duration * retryCount);
        //构建消费对象数据
        RabbitMqPlusConsumerTaskPO consumerTaskPO = new RabbitMqPlusConsumerTaskPO();
        consumerTaskPO.setConsumerTime(DateUtils.currentTime());
        consumerTaskPO.setRetryCount(retryCount);
        consumerTaskPO.setStatusCode(ProcessStatusEnum.TODO.getCode());
        consumerTaskPO.setStatusDesc(ProcessStatusEnum.TODO.getDesc());
        consumerTaskPO.setNextExecuteTime(nextExecuteTime);
        consumerTaskPO.setQueueName(consumerDTO.getQueueName());
        consumerTaskPO.setConsumerTypeCode(consumerDTO.getConsumerTypeCode());
        return consumerTaskPO;
    }
    
    
    public static RabbitMqPlusConsumerTaskPO buildConsumerProcessorSuccess(int retryCount,RabbitMqPlusConsumerDTO consumerDTO) {
        RabbitMqPlusConsumerTaskPO consumerTaskPO = new RabbitMqPlusConsumerTaskPO();
        consumerTaskPO.setConsumerTime(DateUtils.currentTime());
        consumerTaskPO.setRetryCount(retryCount);// 计算逻辑在调用方
        consumerTaskPO.setStatusCode(ProcessStatusEnum.DONE.getCode());
        consumerTaskPO.setStatusDesc(ProcessStatusEnum.DONE.getDesc());
        consumerTaskPO.setQueueName(consumerDTO.getQueueName());
        consumerTaskPO.setConsumerTypeCode(consumerDTO.getConsumerTypeCode());
        return consumerTaskPO;
    }

}
